﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KipmeHQ;
using System.Web.Script.Services;
using System.Web.Services;

public partial class Magazine : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            magazinedproducts[] magProducts;
            magProducts = service.getMagazinedProducts();
            if (!Page.IsPostBack)
            {
                List<productType> productTypes = new List<productType>();
                productTypes.Add(new productType());
                productTypes.Reverse();
                productTypes.AddRange(service.getProductTypes());
                TypeFilterDropDownList.DataSource = productTypes;
                TypeFilterDropDownList.DataBind();

                List<category> productsCategories = new List<category>();
                productsCategories.Add(new category());
                productsCategories.Reverse();
                productsCategories.AddRange(service.getCategories());
                CategoryFilterDropDownList.DataSource = productsCategories;
                CategoryFilterDropDownList.DataBind();

                ddlType.DataSource = productTypes.ToList().GetRange(1, productTypes.Count - 1);
                ddlType.DataBind();
                ddlCategory.DataSource = productsCategories.ToList().GetRange(1, productsCategories.Count - 1);
                ddlCategory.DataBind();

                itemsList.DataSource = magProducts;
                itemsList.DataBind();
            }
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void btnApplyFilter_Click(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            magazinedproducts[] magProducts = service.getMagazinedProducts();
            if (CategoryFilterDropDownList.SelectedIndex != 0)
            {
                magProducts = magProducts.Where(x => x.category.Equals(CategoryFilterDropDownList.SelectedValue)).ToArray();
            }
            if (TypeFilterDropDownList.SelectedIndex != 0)
            {
                magProducts = magProducts.Where(x => x.type.Equals(TypeFilterDropDownList.SelectedValue)).ToArray();
            }

            itemsList.DataSource = magProducts;
            itemsList.DataBind();
            dpListView.SetPageProperties(0, dpListView.PageSize, true);
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void btnClearFilters_Click(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            magazinedproducts[] magProducts = service.getMagazinedProducts();
            itemsList.DataSource = magProducts;
            itemsList.DataBind();
            TypeFilterDropDownList.SelectedIndex = 0;
            CategoryFilterDropDownList.SelectedIndex = 0;
            dpListView.SetPageProperties(0, dpListView.PageSize, true);
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void btnAddNewItemUnvisible_Click(object sender, EventArgs e)
    {
        btnAddNewItemUnvisible.Text = "0";
        int amount = -1;
        if (!int.TryParse(txtAmount.Text, out amount))
        {
            return;
        }
        if (amount < 0)
        {
            return;
        }

        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            service.addProductToMagazine(amount, txtName.Text, ddlType.SelectedValue, ddlCategory.SelectedValue, txtDescription.Text);
        }
        catch (System.Exception ex)
        {
            btnAddNewItemUnvisible.Text = "-1";
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }

    protected void itemsList_PagePropertiesChanged(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            magazinedproducts[] magProducts = service.getMagazinedProducts();
            if (CategoryFilterDropDownList.SelectedIndex != 0)
            {
                magProducts = magProducts.Where(x => x.category.Equals(CategoryFilterDropDownList.SelectedValue)).ToArray();
            }
            if (TypeFilterDropDownList.SelectedIndex != 0)
            {
                magProducts = magProducts.Where(x => x.type.Equals(TypeFilterDropDownList.SelectedValue)).ToArray();
            }
            itemsList.DataSource = magProducts;
            itemsList.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }
}